<template>
  <el-dialog title="品种详细" :close-on-click-modal="false" :visible.sync="visible" fullscreen center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
			<div class="avue-crud">
        <el-table :data="dataList" height="calc(100vh - 205px)" ref="table" border show-summary :summary-method="getSummaries" v-loading="dataListLoading">
          <el-table-column key="1" prop="goodsCode" label="商品编码" width="180" fixed="left">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="getDetailByCheck(scope.row)">{{scope.row.goodsCode}}</el-button>
            </template>
          </el-table-column>
          <el-table-column key="2" prop="goodsName" label="商品名称" min-width="230" fixed="left"></el-table-column>
          <el-table-column key="3" prop="goodsQuantity" label="数量" width="100"></el-table-column>

          <el-table-column key="4" prop="ctgName" label="商品类别" width="100"></el-table-column>
          <el-table-column key="5" prop="supplyName" label="制造商" width="100"></el-table-column>
          <el-table-column key="6" prop="goodsPrice" label="标准零售价" width="100"></el-table-column>
          <el-table-column key="7" prop="stockName" label="仓位" width="200"></el-table-column>
          <el-table-column key="8" prop="batch" label="批次" width="100"></el-table-column>
          <el-table-column key="9" prop="invalidStr" label="效期" width="110"></el-table-column>
        </el-table>
</div>

      <div class="avue-crud__pagination" style="margin-bottom: 25px;margin-top: 0;">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100, 200]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <goods-info v-if="infoVisible" ref="info"></goods-info>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams } from '@/util/tools'
  import { inventoryDetailFetchList, getStorageGoodsNumberSum } from '@/api/eims/storage'
  import { getCtgList } from '@/api/eims/ctg'
  import { getStockList } from '@/api/eims/stock'

  import GoodsInfo from '../goodsInfo/detail'

  export default {
    data () {
      return {
        visible: false,
        infoVisible: false,
        checked: false,
        dataForm: {
          quantityCheck: 1,
          queryPoint: '1',
          groupFlag: '1',
          stockIds: []
        },
        radio: '1',
        allGoodsNum: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,

        time: '',

        dataListLoading: false
      }
    },
    components: {
      GoodsInfo
    },
    computed: {
      ...mapGetters(['permissions','bizData'])
    },
    methods: {
      init(data,time,stockIds){
        
        this.visible = true
        this.time = time

        this.dataForm.stockIds = stockIds
        this.dataForm.goodsCode = data.goodsCode
        this.dataForm.goodsPriceMax = data.goodsPrice
        this.dataForm.goodsPriceMin = data.goodsPrice
        this.getDataList()
      },
      //  查询角度
      queryPointChangeHandle(val) {
        if(val == '3'){
          // this.dataForm.ctgId = 1
          this.$set(this.dataForm,'ctgId',1)
        }
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        // this.dataForm.quantityCheck = 1;
        if(this.dataForm.queryPoint == 3){
          if(!this.dataForm.ctgId){
            this.$message.warning('请选择商品类型')
            return
          }
        }
        if(this.dataForm.ctgId == '3' || this.dataForm.ctgId == '4'){
          this.dataForm.customizeType = this.dataForm.customizeType
        }
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = filterParams(this.dataForm)
        this.dataListLoading = true
        inventoryDetailFetchList(Object.assign(params1,params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
          this.$nextTick(() => {
             this.$refs.table.doLayout()
          })
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
        this.$nextTick(() => {
          this.$refs.table.bodyWrapper.scrollTop = 0;
        })

      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
        this.$nextTick(() => {
          this.$refs.table.bodyWrapper.scrollTop = 0;
        })
      },

      //根据code获取商品详细信息
      getDetailByCheck(info){
        this.infoVisible = true
        this.$nextTick(() => {
          this.$refs.info.init(info,'商品信息',this.time,this.dataForm.stockIds)
        })
      },
      //  合计
      getSummaries(param) {
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if(index === 0) {
            sums[index] = '总库存合计:' + this.allGoodsNum
            return;
          }

          let idx = 0
          if(this.dataForm.queryPoint == '1' || this.dataForm.queryPoint == '2'){
            idx = 2
          }else{
            idx = 3
          }

          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            if(index == idx){
              sums[index] = '合计：' + values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return prev + curr;
                } else {
                  return prev;
                }
              }, 0)
            }
          }
        });
        return sums;
      },
      selectAll(){
        this.dataForm.stockIds = []
        if(this.checked) {
          this.stockOptions.map((item) => {
            this.dataForm.stockIds.push(item.id)
          })
        }else{
          this.dataForm.stockIds = []
        }
      },
      changeSelect(val) {
        if (val.length === this.stockOptions.length) {
          this.checked = true
        } else {
          this.checked = false
        }
      },
    }
  }
</script>
<style lang="scss" scoped>
  /deep/ .el-table {
  display: flex;
  flex-direction: column;
  }

  /deep/ .el-table__body-wrapper {
    // order: 1;
    z-index: 2;
  }
  .bg-row {
    padding-left: 5px;
    padding-top: 5px;
  }
  .el-checkbox {
    text-align: right;
    width: 100%;
    padding-right: 20px;
  }
</style>
